package web;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.IAlbum;
import model.IArtist;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import dao.IArtistDAO;

/**
 * @spring.bean name="albumListController"
 * @spring.property name="artistService" ref="ARTIST_SERVICE"
 * @author ipeel
 *
 */
public class AlbumListController implements Controller {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());
    private IArtistDAO artistService;
    
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String artistId = request.getParameter("artistId");
    	IArtist artist = getArtistService().getArtistbyId(new Integer(artistId));
    	List<IAlbum> albums = new ArrayList<IAlbum>(artist.getAlbums());
    	
    	Map<String, Object> beans = new HashMap<String, Object>();
    	beans.put("albums", albums);
    	
    	if(albums.size() > 0) {
    		beans.put("artist", albums.get(0).getArtist());
    	}
    	
        return new ModelAndView(IConstants.Views.ALBUM_LIST, beans);
    }

	/**
	 * @return Returns the artistService.
	 */
	public IArtistDAO getArtistService() {
		return artistService;
	}

	/**
	 * @param artistService The artistService to set.
	 */
	public void setArtistService(IArtistDAO artistService) {
		this.artistService = artistService;
	}
}